<?php

namespace addons\Purchase\common\models;

use Yii;

/**
 * This is the model class for table "{{%addon_oa_purchase}}".
 *
 * @property int $id ID
 * @property int $type 类型
 * @property int $amount 金额
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 修改时间
 */
class Purchase extends \common\models\base\BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%addon_purchase}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['cate_id', 'amount'], 'required'],
            [['merchant_id','member_id','cate_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['amount'], 'number', 'min' => 0,],
            [['mark'], 'string', 'max' => 255],
            [['date'], 'safe'],

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'member_id' => '用户',
            'cate_id' => '采购类型',
            'amount' => '金额',
            'date' => '采购日期',
            'mark' => '备注',
            'status' => '状态',
            'created_at' => '添加时间',
            'updated_at' => '修改时间',
        ];
    }
    /**
     * 关联用户
     *
     * @return \yii\db\ActiveQuery
     */
    public function getMember()
    {
        return $this->hasOne(\common\models\merchant\Member::class, ['id' => 'member_id']);
    }
    public function getCate()
    {
        return $this->hasOne(PurchaseCate::class, ['id' => 'cate_id']);
    }
    public function beforeSave($insert)
    {
        if(Yii::$app->id == 'backend'||Yii::$app->id == 'merchant')
            $this->member_id = Yii::$app->user->id;
        else
            $this->member_id = Yii::$app->user->identity->member_id;
        $this->merchant_id = Yii::$app->user->identity->merchant_id;
        return parent::beforeSave($insert);
    }

}